package com.project.controller.web;

import com.project.common.utils.MyPageHelper;
import com.project.common.utils.PublicUtils;
import com.project.entity.Role;
import com.project.myBeans.R;
import com.project.myBeans.ResponseBean;
import com.project.service.RoleService;
import com.project.service.base.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/5/27 16:42
 * @param:
 * @return:
 */
@EnableAutoConfiguration //自动载入应用程序所需的所有Bean
@RestController //@Controller与@ResponseBody的合并
@RequestMapping(value = "/role")
@Api(description = "角色相关",value = "role")
public class RoleController extends BaseService{
    @Autowired
    private RoleService roleService;
    /**
     * TateBrown 2018年5月27日
     *
     * @param role
     * @return
     */
    @ApiOperation(" 添加单条角色表记录")
    @PostMapping("/addOne")
    public R addOne(@RequestBody Role role){
        try {
            roleService.addOne(role);
            return R.ok("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
         return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id删除单条记录")
    @GetMapping("/removeOneById/{id}")
    public R removeOneById(@PathVariable("id") Integer id){
        try {
            Assert.isTrue(roleService.removeOneById(id) > 0, "删除失败");
            return R.ok("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
          return R.error(e.getMessage());
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param ids
     * @return
     */
    @ApiOperation("根据id删除单条记录")
    @PostMapping("/removeBatchByIdList/{ids}")
    public R removeBatchByIdList(@PathVariable("ids") String ids){
        List<Integer> idList = PublicUtils.getIntIdList(ids, ",");
        try {
            if (idList.size() > 0)
                roleService.removeBatchByIdList(idList);
            return R.ok("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("操作失败");
        }
    }
/**
 * TateBrown 2018年5月27日
 *
 * @param role
 * @return
 */
    @ApiOperation("根据角色表主键id修改单条记录")
    @PostMapping("/modifyOneById")
    public R modifyOneById(@RequestBody Role role){
        try {
            Assert.isTrue(roleService.modifyOneById(role) > 0, "修改失败");
            return R.ok("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.ok(e.getMessage());
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param role
     * @return
     */
    @ApiOperation("根据条件模糊查找")
    @PostMapping("/findBatchByCondition")
    public R findBatchByCondition(@RequestBody Role role){
        try {
            role.setPageIndex(role.getPageIndex() - 1);
            return R.ok("查找成功").put("data",roleService.findBatchByCondition(role)).put("total", MyPageHelper.getTotal(roleService.findBatchByCondition(role)));
        } catch (Exception e) {
            e.printStackTrace();
           return R.error("程序异常");
        }
    }

    /**
     * TateBrown 2018年5月27日
     *
     * @param id
     * @return
     */
    @ApiOperation("根据主键查找单个角色")
    @GetMapping("/findOneById/{id}")
    public R findOneById(@PathVariable Integer id){
        try {
            return R.ok("查找成功").put("data",roleService.findOneById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("程序异常");
        }
    }
}
